<?php

namespace App\Http\Controllers\Api\V1\Frontend;


use App\Http\Controllers\BaseController;
use App\Http\Requests\Api\V1\Customer\Follow\CreateRequest;
use App\Models\Customer\Follow;
use App\Transformers\Frontend\FollowTransformer;

class FollowController extends BaseController
{
    public function index()
    {
        $follow = Follow::where('author_id', \Auth::user()->id)->paginate(15);
        return $this->response->paginator($follow, new FollowTransformer());
    }

    public function store(CreateRequest $request)
    {
        \DB::transaction(function () use ($request) {
            $attributes = $request->validated();
            $attributes['author_id'] = \Auth::user()->id;
            $model = Follow::where($attributes)->first();
            if ($model) {
                $model->delete();
            } else {
                $model = Follow::create($attributes);
            }
        });
        return [
            'code' => '0000', 'message' => '操作成功', 'data' => true
        ];
    }
}
